
#include "AccountStateRestricted.h"
#include "AccountStateNormal.h"
#include "AccountStateOverdraft.h"
#include "Account.h"
#include <iostream>

using namespace npState;


//other constructors
AccountStateRestricted::AccountStateRestricted(weak_ptr<Account> account) : AccountState()
{
    this->setAccount(account);
}

//public methods
void AccountStateRestricted::deposit(double amount) throw(AccountState::ErrorEnum)
{
    if (shared_ptr<Account> acc = this->getAccount().lock())
    {
        acc->setBalance(acc->getBalance() + amount);
        this->stateCheck();
    }
    else
    {
        throw AccountState::ErrorEnum::AccountIsNull;
    }
}

void AccountStateRestricted::withdraw(double amount) throw(AccountState::ErrorEnum)
{
    if (shared_ptr<Account> acc = this->getAccount().lock())
    {
        cout << "账号受限，取款失败。" << flush;
    }
    else
    {
        throw AccountState::ErrorEnum::AccountIsNull;
    }
}

void AccountStateRestricted::calcInterest() throw(AccountState::ErrorEnum)
{
    if (shared_ptr<Account> acc = this->getAccount().lock())
    {
        cout << "计算利息" << endl;
    }
    else
    {
        throw AccountState::ErrorEnum::AccountIsNull;
    }
}

void AccountStateRestricted::stateCheck() throw(AccountState::ErrorEnum)
{
    if (shared_ptr<Account> acc = this->getAccount().lock())
    {
        if (acc->getBalance() > 0)
        {
            acc->setState(make_shared<AccountStateNormal>(acc));
        }
        else if (acc->getBalance() > -2000)
        {
            acc->setState(make_shared<AccountStateOverdraft>(acc));
        }
    }
    else
    {
        throw AccountState::ErrorEnum::AccountIsNull;
    }
}

string AccountStateRestricted::toString() const throw()
{
    return "Restricted";
}

//protected methods

//private methods

//static part

//others
